package com.chainpattern;

import javax.servlet.*;
import javax.servlet.FilterChain;
import java.io.IOException;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;

/**
 * @author luo
 * @since 2022/8/22 14:55
 */
public class FilterManager implements Filter {

    private final List<Filter> filterList = new ArrayList<>();

    public void add(Filter filter) {
        filterList.add(filter);
    }

    @Override
    public void doFilter(ServletRequest request, ServletResponse response, FilterChain chain) throws IOException, ServletException {


        MyFilterChain myFilterChain = new MyFilterChain();
        myFilterChain.doFilter(request, response);

    }

    class MyFilterChain implements FilterChain {


        Iterator<Filter> iterator = filterList.iterator();


        public MyFilterChain() {

        }

        @Override
        public void doFilter(ServletRequest request, ServletResponse response) throws IOException, ServletException {
            if (iterator.hasNext()) {
                Filter filter = iterator.next();
                filter.doFilter(request, response, this);
            }
        }
    }

}



